package com.booksystem.controller;

import com.booksystem.common.RestResult;
import com.booksystem.dto.ProductDTO;
import com.booksystem.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品管理控制器
 * </p>
 * @author 段伟豪
 */
@Api("商品管理")
@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 新增商品
     *
     * @param productDTO 商品信息
     * @return RestResult结果
     */
    @PostMapping("/add")
    @ApiOperation("新增商品")
    public RestResult<Boolean> addProduct(@RequestBody @Valid ProductDTO productDTO) {
        try {
            Boolean result = productService.addProduct(productDTO);
            return new RestResult<>("000000", "调用成功", result);
        } catch (RuntimeException e) {
            return new RestResult<>("000001", e.getMessage(), false);
        }
    }

    /**
     * 编辑商品
     *
     * @param productDTO 商品信息
     * @return RestResult结果
     */
    @PutMapping("/edit")
    @ApiOperation("编辑商品")
    public RestResult<Boolean> editProduct(@RequestBody @Valid ProductDTO productDTO) {
        try {
            Boolean result = productService.editProduct(productDTO);
            return new RestResult<>("000000", "调用成功", result);
        } catch (RuntimeException e) {
            return new RestResult<>("000001", e.getMessage(), false);
        }
    }

    /**
     * 删除商品
     *
     * @param productId 商品ID
     * @return RestResult结果
     */
    @DeleteMapping("/delete/{productId}")
    @ApiOperation("删除商品")
    public RestResult<Boolean> deleteProduct(@PathVariable Long productId) {
        try {
            Boolean result = productService.deleteProduct(productId);
            return new RestResult<>("000000", "调用成功", result);
        } catch (RuntimeException e) {
            return new RestResult<>("000001", e.getMessage(), false);
        }
    }

    /**
     * 上架商品
     *
     * @param productId 商品ID
     * @return RestResult结果
     */
    @PutMapping("/putOnSale/{productId}")
    @ApiOperation("上架商品")
    public RestResult<Boolean> putOnSale(@PathVariable Long productId) {
        try {
            Boolean result = productService.putOnSale(productId);
            return new RestResult<>("000000", "调用成功", result);
        } catch (RuntimeException e) {
            return new RestResult<>("000001", e.getMessage(), false);
        }
    }

    /**
     * 下架商品
     *
     * @param productId 商品ID
     * @return RestResult结果
     */
    @PutMapping("/putOffSale/{productId}")
    @ApiOperation("下架商品")
    public RestResult<Boolean> putOffSale(@PathVariable Long productId) {
        try {
            Boolean result = productService.putOffSale(productId);
            return new RestResult<>("000000", "调用成功", result);
        } catch (RuntimeException e) {
            return new RestResult<>("000001", e.getMessage(), false);
        }
    }
}